﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.DynamicData.ModelProviders;
using NHibernate.Mapping;

namespace HibernateDynamicData.Adapter
{
    public class NHibernateDataModelProvider : DataModelProvider
    {
        public NHibernateDataModelProvider()
        {
            this.ContextType = typeof(GenericNHibernateContext);
            var classMappings = NHibernateFactory.Current.Configuration.ClassMappings;
            this.tables = new List<TableProvider>();
            foreach (PersistentClass item in classMappings)
            {
                var prov = new NHibernateTableProvider(this, item);
                tables.Add(prov);
            }
        }

        public override object CreateContext()
        {
            return new GenericNHibernateContext();
        }

        private List<TableProvider> tables = new List<TableProvider>();
        public override System.Collections.ObjectModel.ReadOnlyCollection<TableProvider> Tables
        {
            get { return tables.AsReadOnly(); }
        }
    }
}
